//
//  FKBezierPathView.m
//  FKUIBezierPathTestDemo
//
//  Created by 云眼科技 on 16/8/1.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "FKBezierPathView.h"

@interface FKBezierPathView ()
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) CAShapeLayer *shapeLayer1;
@end

@implementation FKBezierPathView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:6.f];
//    [path moveToPoint:CGPointMake(rect.origin.x, rect.origin.y)];
//    [path addLineToPoint:CGPointMake(rect.size.width, rect.origin.y)];
//    
//    
//    path.lineWidth = 1.f;
//    
//    path.miterLimit = 6.f;
//    path.flatness = 6.f;
//    path.lineJoinStyle = kCGLineJoinRound;
//    
//    //c数组，表示绘制1个点，空5个点，绘制10个点，空15个点，再以此循环
//    CGFloat pattern[] = {1, 5, 5, 1};
//    //count表示pattern里的数据个数， phase表示第一段虚线绘制时跳过多少点
//    [path setLineDash:pattern count:1 phase:0];
//    
//    UIColor *strokeColor = [UIColor redColor];
//    UIColor *fillColor = [UIColor lightGrayColor];
//    [strokeColor set];
//    [fillColor set];
//    [path stroke];
    
    
//    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
//    bezierPath.lineWidth = 1;// 线宽
//    [[UIColor redColor] setStroke];// 线条颜色
//    // 设置起始位置
//    [bezierPath moveToPoint:rect.origin];
//    [bezierPath addLineToPoint:CGPointMake(rect.size.width, rect.size.width)];
//    [bezierPath stroke];
//    
//    CGFloat radius = (rect.size.width > rect.size.height ? rect.size.height : rect.size.width)/2;
//    
//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rect.size.width/2, rect.size.height/2) radius:radius startAngle:0 endAngle:2*M_PI clockwise:YES];
//    path.lineWidth = 1.f;
//    [[UIColor lightGrayColor] setFill];
//    [path fill];
//    
    
    /**
     *  backgroundView style, such as change the title color, or add a line view at the bottom of the item and so on.
     */
    typedef NS_ENUM(NSInteger, FKBackgroundViewType) {
        /**
         *  add a fill view behind the selected item.
         */
        FKBackgroundViewTypeRoundRectFill,
        /**
         *  add a stroke view behind the selected item.
         */
        FKBackgroundViewTypeRoundRectStroke 
    } NS_ENUM_AVAILABLE_IOS(7_0);
    
    
//    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
//    shapeLayer1.frame = CGRectMake(rect.size.width/2-rect.size.width/5/2, rect.size.height/2-rect.size.width/5/2, rect.size.width/5, rect.size.width/5);
//    shapeLayer1.lineWidth = 2;
//    shapeLayer1.fillColor = [UIColor whiteColor].CGColor;
//    shapeLayer1.strokeColor = [UIColor redColor].CGColor;
//    
//    CGFloat radius = (shapeLayer1.bounds.size.width > shapeLayer1.bounds.size.height ? shapeLayer1.bounds.size.height : shapeLayer1.bounds.size.width)/2;
//
//    UIBezierPath *shapePath1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(shapeLayer1.bounds.size.width/2, shapeLayer1.bounds.size.height/2) radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
//    
//    shapeLayer1.path = shapePath1.CGPath;
//    
//    [self.layer addSublayer:shapeLayer1];
    

//    UIBezierPath *linePath1 = [UIBezierPath bezierPath];   
//    [[UIColor redColor] setStroke];
//    linePath1.lineWidth = 1.f;
//    CGPoint startPoint = CGPointMake(10, 10);
//    CGPoint endPoint = CGPointMake(rect.size.width-10 , rect.size.height-10);
//    [linePath1 moveToPoint:startPoint];
//    [linePath1 addLineToPoint:endPoint];
//    [linePath1 stroke];
//
//    
    self.shapeLayer1 = [CAShapeLayer layer];
    self.shapeLayer1.frame = rect;
    self.shapeLayer1.lineWidth = 2;
    self.shapeLayer1.fillColor = [UIColor whiteColor].CGColor;
    self.shapeLayer1.strokeColor = [UIColor redColor].CGColor;
    CGFloat radius = (self.shapeLayer1.bounds.size.width > self.shapeLayer1.bounds.size.height ? self.shapeLayer1.bounds.size.height : self.shapeLayer1.bounds.size.width)/2;
    self.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.shapeLayer1.bounds.size.width/2, self.shapeLayer1.bounds.size.height/2) radius:radius startAngle:0 endAngle:0 clockwise:YES];
    self.shapeLayer1.path = self.path.CGPath;
    [self.layer addSublayer:self.shapeLayer1];
    
//    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
//    shapeLayer2.frame = CGRectMake(rect.size.width-rect.size.width/5, rect.size.height-rect.size.height/5, rect.size.width/5, rect.size.width/5);
//    shapeLayer2.lineWidth = 2;
//    shapeLayer2.fillColor = [UIColor brownColor].CGColor;
//    shapeLayer2.strokeColor = [UIColor redColor].CGColor;
//    CGFloat radius2 = (shapeLayer2.bounds.size.width > shapeLayer2.bounds.size.height ? shapeLayer2.bounds.size.height : shapeLayer2.bounds.size.width)/2;
//    UIBezierPath *shapePath2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(shapeLayer2.bounds.size.width/2, shapeLayer2.bounds.size.height/2) radius:radius2 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//    shapeLayer2.path = shapePath2.CGPath;
//    [self.layer addSublayer:shapeLayer2];
    
    

}

-(void)setProgress:(CGFloat)progress{
    _progress = progress;
    if (progress > 2 * M_PI) return;
    CGFloat radius = (self.shapeLayer1.bounds.size.width > self.shapeLayer1.bounds.size.height ? self.shapeLayer1.bounds.size.height : self.shapeLayer1.bounds.size.width)/2;
    self.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.shapeLayer1.bounds.size.width/2, self.shapeLayer1.bounds.size.height/2) radius:radius startAngle:0 endAngle:progress clockwise:YES];
    self.shapeLayer1.path = self.path.CGPath;
}

@end
